    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.logic;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;

import ch.zarzu.champions.builder.gui.Characteristic;

public class CharacteristicObserver {
    private HashMap<String, HashSet<Characteristic>> subscribers;
    private HashMap<String, Double> characteristics;
    
    private static CharacteristicObserver instance = null;
    
    private CharacteristicObserver() {
        subscribers = new HashMap<String, HashSet<Characteristic>>();
        characteristics = new HashMap<String, Double>();
    }
    
    //Characteristics subscribe to this observers to be updated
    public void subscribe(Characteristic c) {
        HashSet<Characteristic> set;
        if(subscribers.containsKey(c.getName()))
            set = subscribers.get(c.getName());
        else
            set = new HashSet<Characteristic>();
        set.add(c);
        subscribers.put(c.getName(), set);
    }
    
    public void update(HashMap<String, Double> map, HashMap<String, HashMap<String, Double>> map2) {
        characteristics = map;
        for(String k : map.keySet()) {
            if(subscribers.containsKey(k)) {
                for(Characteristic c : subscribers.get(k)) {
                    c.setValue(map.get(k));
                    if(map2.containsKey(k))
                        c.setTooltip(map2.get(k));
                    else
                        c.clearTooltip();
                }
            }
        }
    }
    
    public void update(HashMap<String, Double> map) {
        characteristics = map;
        for(String k : map.keySet()) {
            if(subscribers.containsKey(k)) {
                for(Characteristic c : subscribers.get(k))
                    c.setValue(map.get(k));
            }
        }
    }
    
    public boolean hasCharacteristic(String s) {
        return characteristics.containsKey(s);
    }
    
    public Double getCharacteristic(String s) {
        return characteristics.get(s);
    }
    
    public String getFormula(String s) {
        if(subscribers.containsKey(s)) {
            for(Characteristic c : subscribers.get(s))
                return c.getFormula();
        }
        return "";
    }
    
    public LinkedList<String> getChildren(String s) {
        if(subscribers.containsKey(s)) {
            for(Characteristic c : subscribers.get(s))
                return (LinkedList<String>) c.getChildren().clone();
        }
        return new LinkedList<String>();
    }
    
    public String getConcat(String s) {
        if(subscribers.containsKey(s))
            for(Characteristic c : subscribers.get(s))
                return c.getConcat();
        return "";
    }
    
    public boolean isStat(String s) {
        for(Characteristic c : subscribers.get(s))
            return c.isStat();
        return false;
    }
    
    /**
     * updates all characteristics to the current language
     */
    public void updateLanguage() {
        for(HashSet<Characteristic> set : subscribers.values()) {
            for(Characteristic c : set)
                c.updateLanguage();
        }
    }
    
    public static CharacteristicObserver getInstance() {
        if(instance == null)
            instance = new CharacteristicObserver();
        return instance;
    }
}
